package com.ms.teapuesto.util;


import org.apache.log4j.Logger;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.StringUtils;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

@Deprecated
public class MailUtil {

    private final static Logger log = Logger.getLogger(MailUtil.class);

    public static void main(String[] args) throws Exception {

        MailAuthBean mailAuthBean = new MailAuthBean("mail.agile-works.com", "ryky.azanero@agile-works.com", "$$sistemas$$", "rickykaiser@gmail.com");
        send(mailAuthBean, "TEST DPWC", "ddd", null, null);

/*
        MailAuthBean mailAuthBean = new MailAuthBean("smtp.gmail.com", "rickykaiser@gmail.com", "rkac41316853", "rickykaiser@gmail.com");
        send(mailAuthBean, "TEST DPWC", "ddd", null, null);

*/
/*
        MailAuthBean mailAuthBean = new MailAuthBean("192.168.0.167", "emisorsunat@ebizlatin.com", "emisorsunat", "emisorsunat@ebizlatin.com");
        send(mailAuthBean, "TEST DPWC", "ddd", null, null);
*/
    }

    public static void send( String to, String subject, String text, Map<String, File> filesMap, Map<String, File> imagesMap) throws Exception {
        send( to, "", subject, text, filesMap, imagesMap);
    }

    public static void sendCco(String to, String cco, String subject, String text, Map<String, File> filesMap, Map<String, File> imagesMap) throws Exception {
        MailAuthBean mailAuthBean =new MailAuthBean("192.168.0.167", "emisorsunat@ebizlatin.com", "emisorsunat", "emisorsunat@ebizlatin.com");
        if (StringUtils.hasText(cco)) {
            mailAuthBean.setCco(cco);
        }
        send(mailAuthBean, subject, text, filesMap, imagesMap);
    }

    public static void send(String to, String cc, String subject, String text, Map<String, File> filesMap, Map<String, File> imagesMap) throws Exception {
        MailAuthBean mailAuthBean =new MailAuthBean("192.168.0.167", "emisorsunat@ebizlatin.com", "emisorsunat", "emisorsunat@ebizlatin.com");
        if (StringUtils.hasText(cc)) {
            mailAuthBean.setCc(cc);
        }
        send(mailAuthBean, subject, text, filesMap, imagesMap);
    }

    private static void send(MailAuthBean mailAuthBean, String subject, String text, Map<String, File> filesMap, Map<String, File> imagesMap) throws Exception {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");


        javaMailSender.setHost(mailAuthBean.getHost());
        javaMailSender.setPort(587);
        javaMailSender.setUsername(mailAuthBean.getUsr());
        javaMailSender.setPassword(mailAuthBean.getPwd());
        Properties properties = javaMailSender.getJavaMailProperties();
        int timeout = 10000;
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.connectiontimeout", timeout);
        properties.put("mail.smtp.timeout", timeout);
        properties.put("mail.smtp.class", "com.sun.mail.smtp.SMTPTransport");
        properties.put("mail.smtp.starttls.enable", "true");

        javaMailSender.setJavaMailProperties(properties);

        mimeMessageHelper.setFrom(mailAuthBean.getFrom());
        mimeMessageHelper.setTo(splitEmailAddress(mailAuthBean.getTo()));
        String cc = mailAuthBean.getCc();
        if (StringUtils.hasText(cc)) {
            mimeMessageHelper.setCc(splitEmailAddress(mailAuthBean.getCc()));
        }

        String cco = mailAuthBean.getCco();
        if (StringUtils.hasText(cco)) {
            mimeMessageHelper.setBcc(splitEmailAddress(mailAuthBean.getCco()));
        }

        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(text, true);

        if (filesMap != null) {
            for (Entry<String, File> element : filesMap.entrySet()) {
                File file = element.getValue();
                mimeMessageHelper.addAttachment(element.getKey().toString(), file);
            }
        }

        if (imagesMap != null) {
            for (Entry<String, File> element : imagesMap.entrySet()) {
                File image = element.getValue();
                mimeMessageHelper.addInline(element.getKey().toString(), image);
            }
        }

        javaMailSender.send(mimeMessage);

        log.info("Se envió el correo satisfactoriamente a:" + mailAuthBean.getTo() + " subject:" + subject + " body:\n" + text);
    }

    private static String[] splitEmailAddress(String to) {
        return to.split(";");
    }
}